<?php

namespace app\admin\controller;

use app\admin\model\AppLog;
use app\common\LogService;

class App extends Base
{
    // App基本信息
    public function appBase()
    {
        $data = config('app_base');
        return view('app/index', ['data' => $data]);
    }
    // App应用预览
    public function appImg()
    {
        // 获取配置
        $config = config('app_img');

        // 提取所有img字段
        $images = [];
        foreach ($config as $key => $value) {
            if (strpos($key, 'img') === 0 && is_numeric(substr($key, 3)) && !empty($value)) {
                $images[] = $value;
            }
        }

        return view('app/img', ['images' => $images]);
    }
    // App下载设置
    public function appDownload()
    {
        $data = config('app_download');
        return view('app/download', ['data' => $data]);
    }
    // App 更新历史
    public function appUpgrade()
    {
        return view('app/upgrade_log');
    }
    public function upgradeLog()
    {
        $list = AppLog::order('id desc')->paginate();
        return success($list);
    }
    public function upgradeLogAdd()
    {
        $param = input();
        $res = AppLog::create($param);
        if ($res) return success();
        return error();
    }
    public function upgradeLogEdit()
    {
        $param = input();
        $res = AppLog::update($param);
        if ($res) return success();
        return error();
    }
    public function upgradeLogDel()
    {
        $param = input();
        $res = AppLog::destroy($param['id']);
        if ($res) return success();
        return error();
    }
    public function update($conf)
    {
        $arr = ['app_base', 'app_download', 'app_img'];
        $recordLog = function ($status, $message = '') use ($conf) {
            LogService::record(
                name: '修改App配置',
                target_id: $conf,
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : $message,
                user_type: 2 
            );
        };
        if (!in_array($conf, $arr)) {
            $recordLog(0, ['reason' => '请求异常']);
            return error('请求异常');
        }
        $original_data = config($conf);
        if (request()->isPost()) {
            $data = input();
            // 配置文件路径
            $configFile = getAppConfPath('admin') . $conf . '.php';

            // 生成新的配置文件内容
            $content = "<?php\nreturn [\n";
            foreach ($data as $key => $value) {
                if (is_string($value)) {
                    $content .= "    '{$key}' => '{$value}',\n";
                } else {
                    $content .= "    '{$key}' => {$value},\n";
                }
            }
            $content .= "];";
            // 写入配置文件
            if (file_put_contents($configFile, $content)) {
                $recordLog(1, ['original_data' => $original_data]);
                return success();
            } else {
                $recordLog(0);
                return error('配置更新失败');
            }
        }
    }
    // 上传图片
    public function uploadImg()
    {
        $file =  request()->file();
        if (!$file) {
            return error('请上传jpg,png,jpeg后缀图片');
        }
        try {
            validate(['images' => 'fileSize:5242880|fileExt:jpg,png,jpeg'])
                ->check($file);
            if (empty($file)) return error();
            $images = \think\facade\Filesystem::disk('images')->putFile('system', $file['images']);
            if (!$images) return error('图片上传失败');
            $images = '/static/images/' . str_replace('\\', '/', $images);
            return success($images);
        } catch (\think\exception\ValidateException $e) {
            return error($e->getMessage());
        }
    }
    public function uploadApp()
    {
        $file =  request()->file();
        if (!$file) {
            return error('请上传jpg,png,jpeg后缀图片');
        }
        try {
            validate(['file' => 'fileExt:apk'])
                ->check($file);
            if (empty($file)) return error();
            $images = \think\facade\Filesystem::putFile('app', $file['file']);
            if (!$images) return error('文件上传失败');
            $images = '/static/' . str_replace('\\', '/', $images);
            return success($images);
        } catch (\think\exception\ValidateException $e) {
            return error($e->getMessage());
        }
    }
}
